package net.itgoo.parkingpay.ui.edit;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import net.itgoo.parkingpay.rest.ParkingVolleyManager;
import net.itgoo.parkingpay.rest.model.ParkingCardModel;
import net.itgoo.parkingpay.vendor.mvp.ParkingMVPLoadDataCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ParkingEditDataRepository implements ParkingEditContract.DataSource {

    private List<String> mData = new ArrayList<>();
    private HashMap<String, ParkingCardModel> mCardData = new HashMap<>();

    @Override
    public void getPlates(String userId, final ParkingMVPLoadDataCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userId", userId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ParkingVolleyManager.sendJsonObjectRequest(Request.Method.POST,
                ParkingVolleyManager.baseUrl + "App_getplate.do", jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        mData.clear();
                        try {
                            JSONArray array = response.getJSONArray("list");
                            for (int i = 0; i < array.length(); ++i) {
                                JSONObject obj = array.getJSONObject(i);
                                mData.add(obj.getString("plateno"));
                                if (obj.has("card")) {
                                    JSONObject cardObj = obj.optJSONObject("card");
                                    if (cardObj != null) {
                                        mCardData.put(obj.getString("plateno"), ParkingCardModel.parse(cardObj));
                                    }
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        callback.onDataLoaded(mData);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onDataError(error.getMessage());
                    }
                });
    }

    @Override
    public void addPlate(String userId, String plate, final ParkingMVPLoadDataCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("app_userid", userId);
            jsonObject.put("plateval", plate);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ParkingVolleyManager.sendJsonObjectRequest(Request.Method.POST,
                ParkingVolleyManager.baseUrl + "App_addplate.do", jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        int code = 0;
                        String msg = "";
                        try {
                            code = response.getInt("code");
                            msg = response.getString("msg");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (code == 1) {
                            callback.onDataLoaded(response);
                        } else {
                            callback.onDataError(msg);
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onDataError(error.getMessage());
                    }
                });
    }

    @Override
    public void deletePlate(String userId, String plate, final ParkingMVPLoadDataCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("app_userid", userId);
            jsonObject.put("plateval", plate);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ParkingVolleyManager.sendJsonObjectRequest(Request.Method.POST,
                ParkingVolleyManager.baseUrl + "App_delplate.do", jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        int code = 0;
                        try {
                            code = response.getInt("code");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (code == 1) {
                            callback.onDataLoaded(response);
                        } else {
                            callback.onDataError("");
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onDataError(error.getMessage());
                    }
                });
    }

    @Override
    public void updatePlate(String userId, String oldPlate, String newPlate, final ParkingMVPLoadDataCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("app_userid", userId);
            jsonObject.put("plateval", oldPlate);
            jsonObject.put("platenum", newPlate);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ParkingVolleyManager.sendJsonObjectRequest(Request.Method.POST,
                ParkingVolleyManager.baseUrl + "App_updateplate.do", jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        int code = 0;
                        try {
                            code = response.getInt("code");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (code == 1) {
                            callback.onDataLoaded(response);
                        } else {
                            callback.onDataError("");
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onDataError(error.getMessage());
                    }
                });
    }

    @Override
    public List<String> data() {
        return mData;
    }

    @Override
    public String item(int position) {
        if (position >= 0 && position < mData.size()) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public ParkingCardModel card(String plate) {
        if (mCardData.containsKey(plate)) {
            return mCardData.get(plate);
        }
        return null;
    }

    @Override
    public void cancel() {

    }
}
